package com.beiing.tianshuai.tianshuai.message.ui;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.adapter.SearchFriendAdapter;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.entity.SearchFriendBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.message.presenter.SearchFriendPresenter;
import com.beiing.tianshuai.tianshuai.message.view.SearchFriendViewImpl;
import com.beiing.tianshuai.tianshuai.mine.ui.PersonalDataCardActivity;
import com.beiing.tianshuai.tianshuai.util.LogUtils;
import com.beiing.tianshuai.tianshuai.widget.RecycleViewDivider;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

public class SearchFriendActivity extends BaseActivity implements SearchFriendViewImpl{

    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarIvBack;
    @BindView(R.id.toolbar_btn_publish)
    TextView mToolbarBtnSearch;
    @BindView(R.id.et_search_content)
    EditText mEtSearchContent;
    @BindView(R.id.txt_empty_warning)
    TextView mEmptyWarning;
    @BindView(R.id.rv_contact_list)
    RecyclerView mListView;

    private static final String                     TAG = "SearchFriendActivity";
    private Unbinder                                mUnbinder;
    private Context                                 mContext;
    private SearchFriendAdapter                     mAdapter;
    private SearchFriendPresenter                   mPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_friend;
    }

    @Override
    protected void init() {
        mUnbinder = ButterKnife.bind(this);
        mContext = this;
        initToolbar();
        initRecyclerView();
        initListener();
        initPresenter();
    }

    private void initToolbar () {
        mToolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void initRecyclerView() {
        mAdapter = new SearchFriendAdapter(mContext);
        mListView.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        mListView.setNestedScrollingEnabled(false);
        mListView.setLayoutManager(linearLayoutManager);
        //绘制item间的分割线，
        mListView.addItemDecoration(
                new RecycleViewDivider(
                        mContext, LinearLayoutManager.HORIZONTAL, 1,
                        ContextCompat.getColor(mContext, R.color.common_gray_background))
        );
    }

    private void initListener() {
        // 给输入框设置输入监听事件
        mEtSearchContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                String keyWord = editable.toString();
                if (!TextUtils.isEmpty(keyWord)) {
                    searchContact();
                }
            }
        });

        // 给输入框设置软键盘搜索事件
        mEtSearchContent.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                if (i == EditorInfo.IME_ACTION_SEARCH) {
                    if (!TextUtils.isEmpty(mEtSearchContent.getText().toString())) {
                        searchContact();
                        /*隐藏软键盘*/
                        InputMethodManager imm = (InputMethodManager) textView.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                        if (imm.isActive()) {
                            imm.hideSoftInputFromWindow(textView.getApplicationWindowToken(), 0);
                        }
                        return true;
                    } else {
                        showShortText("请输入关键字");
                    }
                }
                return false;
            }
        });

        // 搜索按钮
        mToolbarBtnSearch.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (!TextUtils.isEmpty(mEtSearchContent.getText())) {
                    searchContact();
                }
            }
        });

        // 消息列表
        mAdapter.setListener(
                new SearchFriendAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClickListener(String uid) {
                        Intent intent = new Intent(mContext, PersonalDataCardActivity.class);
                        intent.putExtra("uid", uid);
                        startActivity(intent);
                    }
                }
        );
    }

    private void initPresenter() {
        mPresenter = new SearchFriendPresenter(this);
    }

    private void searchContact() {
        String keyWords = mEtSearchContent.getText().toString();
        mPresenter.getSearchFriendResult(UserInfoBean.getUid(mContext), keyWords);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void onRequestSuccess(SearchFriendBean bean) {
        if (bean.getData().size() > 0) {
            mAdapter.updateData(bean.getData());
            mEmptyWarning.setVisibility(View.GONE);
        } else {
            mAdapter.clearData();
            mEmptyWarning.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onRequestFailed(Throwable throwable) {
        LogUtils.e(throwable.toString(), TAG);
        showShortText("网络异常");
    }
}
